import {getRequest} from "./api";

// 获取菜单数据
export const initMenu = (router, store) => {
    if (store.state.routes.length > 0) {
        return;
    }
    getRequest("/menu").then(data => {
        if (data) {
            let fmtRoutes = formatRoutes(data);
            router.addRoutes(fmtRoutes);
            store.commit('initRoutes', fmtRoutes);
            store.dispatch('connect');
        }
    })
}
export const formatRoutes = (routes) => {
    let fmRoutes = [];
    routes.forEach(router => {
        let {
            path,
            component,
            name,
            keepAlive,
			requireAuth,
            iconCls,
            childrens
        } = router;
		let meta = {
			keepAlive:keepAlive,
			requireAuth:requireAuth
		}
        if (childrens && childrens instanceof Array) {
            childrens = formatRoutes(childrens);
        }
        let fmRouter = {
            path: path,
            name: name,
            iconCls: iconCls,
            meta: meta,
            children: childrens,
            component(resolve) {
				// 首页
                if (component.startsWith("Home")) {
                    require(['../components/' + component + '.vue'], resolve);
                } else if (component.startsWith("Communication")) { // 通信模块
                    require(['../components/communication/' + component + '.vue'], resolve);
                } else if (component.startsWith("Emp")) { // 员工模块
                    require(['../components/emp/' + component + '.vue'], resolve);
                } else if (component.startsWith("Organization")) { // 组织模块
                    require(['../components/organization/' + component + '.vue'], resolve);
                } else if (component.startsWith("Performance")) { // 绩效模块
                    require(['../components/performance/' + component + '.vue'], resolve);
                } else if (component.startsWith("Personnel")) { // 人事模块
                    require(['../components/personnel/' + component + '.vue'], resolve);
                } else if (component.startsWith("Salary")) { // 薪资模块
                    require(['../components/salary/' + component + '.vue'], resolve);
                } else if (component.startsWith("Statistical")) { // 统计模块
                    require(['../components/statistical/' + component + '.vue'], resolve);
                } else if (component.startsWith("System")) { // 系统模块
                    require(['../components/system/' + component + '.vue'], resolve);
                } else if (component.startsWith("User")) { // 用户模块
                    require(['../components/user/' + component + '.vue'], resolve);
                }
            }
        }
        fmRoutes.push(fmRouter);
    })
    return fmRoutes;
}